<?php

class FileManagerItemImage extends FileManagerItem
{
    protected $iconSizes = array();
    protected $defaultIconSize = 64;
    // Default values for size variables. 
    public $thumbWidth = 460;
    public $thumbHeight = 520;
    public $width = 1140;
    public $height = 580;
    public $fitInArea = true;
    
    public function __construct($options=array())
    {
        parent::__construct();
        $this->iconSizes = array("smallIcon" => "32", "mediumIcon" => "64", "largeIcon" => "128");
        $this->file = new Image();
        $this->thumbWidth = $options['thumbWidth'] ? $options['thumbWidth'] : $this->thumbWidth;
        $this->thumbHeight = $options['thumbHeight'] ? $options['thumbHeight'] : $this->thumbHeight;
        $this->width = $options['width'] ? $options['width'] : $this->width;
        $this->height = $options['height'] ? $options['height'] : $this->height;
        $this->fitInArea = $options['fitInArea'] ? $options['fitInArea'] : $this->fitInArea;
    }
    
    protected function loadFile()
    {
        $this->file = new Image();
        $this->file->type = $this->getExtension();
        $this->file->dirPath = $this->_fileDirPath;
        $this->file->urlPath = $this->_fileUrlPath;
        $this->file->filename = $this->getFileName();
        $this->file->file_title = $this->getFileTitle();
        $this->file->type = $this->getExtension();
    }
    
    public function upload($filePost)
    {
        if(parent::upload($filePost))
        {
            return $this->fitInArea ? $this->file->fitInArea($this->width, $this->height) : $this->file->resize($this->width,$this->height);
        }
    }

    public function delete($id = NULL)
    {
        if(parent::delete($id) )
        {
            $this->file->dirPath = $this->_fileDirPath;
            $files = glob($this->_fileDirPath."/".$this->getFileName()."*");
            foreach ( $files as $filename)
            {
                $this->file->filename = basename($filename);
                $this->file->delete();
            }
        }
        $id = $id ? $id : $this->getID();
        if(!$id) return 0;

        if(!$this->file && !$this->get($id))
        {
            $this->_error("Por favor, seleccione un archivo v&aacute;lido.");
            return 0;
        }

        if(!$this->file_name)
        {
            return parent::delete($id);
        }

        $this->file->dirPath = $this->_fileDirPath;
        return 1;
    }
    
    protected function getIconName($type)
    {
        switch($type)
        {
            case "smallIcon":     $postfix = "thumb_small";
                                break;
            case "largeIcon":    $postfix = "thumb_large";
                                break;
            default            :    $postfix = "thumb_medium";
                                break;
        }
        return $this->getFileName()."_".$postfix;    
    }
        
    public function getIconUrl($type = "mediumIcon")
    {        
        $thumbWidth = $thumbHeight = $this->iconSizes[$type] ? $this->iconSizes[$type] : $this->defaultIconSize;    
        $thumb = new Image();
        $thumb->dirPath = $this->file->dirPath;
        $thumb->urlPath = $this->file->urlPath;
        $thumb->filename = $this->getIconName($type);
        if($thumb->exists())
        {
            return $thumb->getUrl();
        }
        $this->file->filename = $this->getFileName();
        if($thumb = $this->file->createThumbnail($thumbWidth, $thumbHeight, $thumb->filename , "", true))
        {
            $thumb->dirPath = $this->file->dirPath;
            $thumb->urlPath = $this->file->urlPath;            
            return $thumb->getUrl();
        }
        return parent::getIconUrl($type);
    }
        
    public function getThumbUrl()
    {
        $thumbWidth = $this->thumbWidth;
        $thumbHeight = $this->thumbHeight;
        $thumb = new Image();
        $thumb->dirPath = $this->file->dirPath;
        $thumb->urlPath = $this->file->urlPath;
        $thumb->filename = $this->getFileName()."_".$this->thumbWidth."x".$this->thumbHeight;
        if($thumb->exists())
        {
            return $thumb->getUrl();
        }
        $this->file->filename = $this->getFileName();
        if($thumb = $this->file->createThumbnail($thumbWidth, $thumbHeight, $thumb->filename, "", true))
        {
            $thumb->dirPath = $this->file->dirPath;
            $thumb->urlPath = $this->file->urlPath;            
            return $thumb->getUrl();
        }
        return $this->getIconUrl("mediumIcon");
    }
    
    public function getThumb($attr = NULL)
    {
        
        if($this->exists())
        {
            return "<img ".$attr." src='".$this->getThumbUrl()."'>";
        }        
    }
    
    public function getImage($attr = NULL)
    {
        if($this->exists())
        {
            return "<img ".$attr." src='".$this->file->getUrl()."'>";
        }        
    }
    
    public function getImageUrl()
    {
        return $this->file->getUrl();
    }        
}
?>
